package com.ruibang.glass.sale.service;

import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DisputeNumberGenerator {
    private static final String DATE_FORMAT = "yyyyMMdd";
    private static final int MAX_SEQUENCE = 9999;

    public static String generateOrderNumber(String baseOrderNumber) {
        if (StringUtils.isEmpty(baseOrderNumber)) {
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            String currentDate = sdf.format(Calendar.getInstance().getTime());
            return currentDate+"0001";
        }
        // 假设输入的baseOrderNumber格式是正确的，并且我们只处理日期和序号部分
        String datePart = baseOrderNumber.substring(0, 8);
        String sequencePart = baseOrderNumber.substring(8);

        // 提取当前日期并格式化为相同的格式
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        String currentDate = sdf.format(Calendar.getInstance().getTime());

        // 如果baseOrderNumber的日期部分与当前日期不同，则重置序号
        if (!datePart.equals(currentDate)) {
            sequencePart = "0001";
        } else {
            // 尝试将序号转换为整数并递增
            int sequence = Integer.parseInt(sequencePart);
            sequence = (sequence % MAX_SEQUENCE) + 1; // 递增并循环到1（如果需要）
            sequencePart = String.format("%04d", sequence); // 格式化为4位数字
        }

        // 合并日期和序号以形成新的单号
        return currentDate + sequencePart;
    }

    public static void main(String[] args) {
        String baseOrderNumber = "202405119999";
        String newOrderNumber = generateOrderNumber(baseOrderNumber);
        System.out.println("Generated Order Number: " + newOrderNumber);
    }
}
